package com.zhht.checkcop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhbc.framework.support.Result;
import com.zhbc.framework.support.Transform;
import com.zhbc.framework.utils.StringUtils;
import com.zhht.checkcop.check.vo.CatalogVO;
import com.zhht.checkcop.check.vo.ClassifyDirectionVO;
import com.zhht.checkcop.check.vo.NovelVO;
import com.zhht.checkcop.service.CatalogService;
import com.zhht.checkcop.service.NovelService;
import com.zhht.checkcop.util.ParamClass;

@Controller
public class NovelCotroller {

	@Autowired
	private NovelService novelService;
	@Autowired
	private CatalogService catalogService;

	private final static Logger log = Logger.getLogger(NovelCotroller.class);

	@RequestMapping(value = "/bookapp/chapter", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String chapter(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		try {
			Map<String, Object> data = p.getData();
			if(data == null){
				log.info("请求参数为null   param："+param);
			}
			if(StringUtils.isStrTrimNull(data.get("nid")+"")){
				return r.failure("请求参数nid不存在");
			}
			if(StringUtils.isStrTrimNull(data.get("sort")+"")){
				return r.failure("请求参数sort不存在");
			}
//			chapter_name
			
			NovelVO novelVO = novelService.getNovelVOById(data.get("nid")+"");
			
			
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("novel", novelVO);
			r.setData(map);
		} catch (Exception e) {
			log.info("查询异常："+e);
			return r.failure("请求失败");
		}
		return r.success();
	}
	
	@RequestMapping(value = "/bookapp/chapter_list", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String chapter_list(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		Map<String, Object> data = p.getData();
		try {
			if(StringUtils.isStrTrimNull(data.get("nid")+"")){
				return r.failure("请求参数nid不存在");
			}
			
			NovelVO novelVO = novelService.getNovelVOById(data.get("nid")+"");
			
			Map<String, ClassifyDirectionVO> classifyTableName = new ParamClass(catalogService).getClassifyTableName();
			ClassifyDirectionVO cdVO=classifyTableName.get(novelVO.getClassify_id());
			if(cdVO==null){
				return r.failure("分类配置异常");
			}
			String tableName = cdVO.getTable_name()+cdVO.getClassify_num();
			
			PageInfo<CatalogVO> listCatalogVO = catalogService.getListByNovelId(novelVO.getId(), tableName, data.get("order")+"", p.getPageNum(), p.getPageSize());
			
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("list", listCatalogVO.getList());
			map.put("nid", novelVO.getId());
			map.put("site", novelVO.getSource());
			map.put("novelName", novelVO.getName());
			
			r.setData(map);
			r.setTotal(listCatalogVO.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			log.info("查询异常："+e);
			return r.failure("请求失败");
		}
		return r.success();
	}
	
	@RequestMapping(value = "/bookapp/search", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String search(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		Map<String, Object> data = p.getData();
		try {
			if(StringUtils.isStrTrimNull(data.get("type")+"")){
				return r.failure("请求参数type不存在");
			}
			if(StringUtils.isStrTrimNull(data.get("word")+"")){
				return r.failure("请求参数word不存在");
			}
			String novelName = data.get("word").toString();
			
			List<NovelVO> list = novelService.getListByName(novelName, p.getPageNum(), p.getPageSize());
			
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("novel", list);
			
			r.setData(map);
			r.setTotal(list.size());
		} catch (Exception e) {
			log.info("查询异常："+e);
			return r.failure("请求失败");
		}
		return r.success();
	}
	
	@RequestMapping(value = "/bookapp/category_novel", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String category_novel(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		Map<String, Object> data = p.getData();
		try {
			String classifyId = data.get("classify_id").toString();
			
			PageInfo<NovelVO> list = novelService.getListByClassify(classifyId, p.getPageNum(), p.getPageSize());
			
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("novel", list);
			r.setData(map);
			r.setTotal(list.getTotal());
		} catch (Exception e) {
			log.info("查询异常："+e);
			return r.failure("请求失败");
		}
		return r.success();
	}
}
